package com.cooksys.film_rental.bo.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooksys.film_rental.bo.AddressBo;
import com.cooksys.film_rental.bo.CityBo;
import com.cooksys.film_rental.bo.CountryBo;
import com.cooksys.film_rental.bo.CustomerBo;
import com.cooksys.film_rental.bo.RegistrationFormBo;
import com.cooksys.film_rental.bo.StoreBo;
import com.cooksys.film_rental.bo.UserBo;
import com.cooksys.film_rental.model.Address;
import com.cooksys.film_rental.model.City;
import com.cooksys.film_rental.model.Country;
import com.cooksys.film_rental.model.Customer;
import com.cooksys.film_rental.model.Store;
import com.cooksys.film_rental.model.User;
import com.cooksys.film_rental.model.forms.RegistrationForm;

@Service("RegistrationFormBo")
public class RegistrationFormBoImpl implements RegistrationFormBo {

	@Autowired
	UserBo userBo;

	@Autowired
	CustomerBo customerBo;

	@Autowired
	AddressBo addressBo;

	@Autowired
	CityBo cityBo;

	@Autowired
	CountryBo countryBo;

	@Autowired
	StoreBo storeBo;

	@Override
	public User saveRegistration(RegistrationForm registrationForm) {

		// Initialize user
		User user = new User();
		String userId = registrationForm.getUserId();
		String password = registrationForm.getPassword();

		// Make sure the user name doesn't already exist in the database
		User existingUserRecord = userBo.getUserByUserId(userId);

		if (existingUserRecord != null) {
			return null;
		}

		user.setUserId(userId);
		user.setPassword(password);

		// Initialize customer
		Customer customer = new Customer();
		String firstName = registrationForm.getFirstName();
		String lastName = registrationForm.getLastName();
		String email = registrationForm.getEmail();

		customer.setFirstName(firstName);
		customer.setLastName(lastName);
		customer.setEmail(email);
		customer.setCreateDate(new Date());

		// Initialize address
		Address address = new Address();
		String addressLine1 = registrationForm.getAddressLine1();
		String addressLine2 = registrationForm.getAddressLine2();
		String district = registrationForm.getDistrict();
		String phoneNumber = registrationForm.getPhoneNumber();
		String postalCode = registrationForm.getPostalCode();

		address.setAddress(addressLine1);
		address.setAddress2(addressLine2);
		address.setDistrict(district);
		address.setPhone(phoneNumber);
		address.setPostalCode(postalCode);

		// Initialize city
		City city = new City();
		String cityName = registrationForm.getCityName();

		city.setCity(cityName);

		// Initialize country
		Country country = new Country();
		String countryId = registrationForm.getCountryId();

		country = countryBo.getCountryByCountryId(countryId);

		// Initialize store
		Store store = new Store();
		String storeId = registrationForm.getStoreId();

		store = storeBo.getStoreByStoreId(storeId);

		// Link city to country
		City existingCityRecord = cityBo.matchCityToDatabase(city, country);

		if (existingCityRecord != null) {
			city = existingCityRecord;
		} else {
			city.setCountry(country);
			city = cityBo.save(city);
		}

		// Link address to city
		Address existingAddressRecord = addressBo.matchAddressToDatabase(
				address, city);

		if (existingAddressRecord != null) {
			address = existingAddressRecord;
		} else {
			address.setCity(city);
			address = addressBo.save(address);
		}

		// Link customer to address and store
		Customer existingCustomerRecord = customerBo.matchCustomerToDatabase(
				customer, address, store);

		if (existingCustomerRecord != null && userBo.getUserByCustomer(existingCustomerRecord) == null) {
			customer = existingCustomerRecord;
		} else {
			customer.setAddress(address);
			customer.setStore(store);
			customer = customerBo.save(customer);
		}

		// Link user to customer
		user.setCustomer(customer);
		user = userBo.save(user);

		return user;
	}
}
